from rest_framework.viewsets import ModelViewSet
from apps.goods.models import SKU,SPU
from apps.meiduo_admin.utils import PageNum
from apps.meiduo_admin.serializers.sku import SKUSerializer,ThreeCategorySerializer,SPUSerializer,SPUSpecificationSerializer
from rest_framework.generics import ListAPIView
from apps.contents.models import GoodsCategory
from apps.goods.models import SPUSpecification,SpecificationOption
from rest_framework.views import APIView
from rest_framework.response import Response


class SKUListModelViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer
    pagination_class = PageNum


class ThreeCategoryListAPIView(ListAPIView):
    # GoodsCategory模型类，是自关联查询，外键的related_name='subs'
    # 二级分类=一级分类.subs
    # 三级分类=二级分类.subs
    # 三级分类.subs的值是None
    # 在GoodsCategory模型类中隐藏字段是goodscategory_set，其实就是subs,所以subs字段的值为None时就是三级分类
    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class = ThreeCategorySerializer


class SPUListAPIView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSerializer


class SPUSpecificationView(APIView):
    def get(self,request,spu_id):
        specs = SPUSpecification.objects.filter(spu_id=spu_id)
        s = SPUSpecificationSerializer(instance=specs,many=True)
        return Response(s.data)


